home *** CD-ROM | disk | FTP | other *** search
/ Inside Multimedia 1994 April / Inside Multimedia CD-ROM (April 1994).iso / prg / gs / gssource.exe / GDEVMEM1.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-24  |  16.4 KB  |  534 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevmem1.c */
  21. /* Generic and monobit "memory" (stored bitmap) device */
  22. /* for Ghostscript library. */
  23. #include "memory_.h"
  24. #include "gs.h"
  25. #include "gxdevice.h"
  26. #include "gxdevmem.h"            /* semi-public definitions */
  27. #include "gdevmem.h"            /* private definitions */
  28.  
  29. /* Define the chunk size for monobit operations. */
  30. #if arch_is_big_endian
  31. #  define mono_chunk uint
  32. #else
  33. #  define mono_chunk ushort
  34. #endif
  35.  
  36. /* ------ Generic code ------ */
  37.  
  38. /* Return the appropriate memory device for a given */
  39. /* number of bits per pixel (0 if none suitable). */
  40. const gx_device_memory *
  41. gdev_mem_device_for_bits(int bits_per_pixel)
  42. {    switch ( bits_per_pixel )
  43.        {
  44.     case 1: return &mem_mono_device;
  45.     case 2: return &mem_mapped2_color_device;
  46.     case 4: return &mem_mapped4_color_device;
  47.     case 8: return &mem_mapped8_color_device;
  48.     case 16: return &mem_true16_color_device;
  49.     case 24: return &mem_true24_color_device;
  50.     case 32: return &mem_true32_color_device;
  51.     default: return 0;
  52.        }
  53. }
  54.  
  55. /* Compute the raster (bytes per scan line). */
  56. uint
  57. gdev_mem_raster(const gx_device_memory *dev)
  58. {    return (((ulong)(dev->width) * dev->color_info.depth + 31) >> 5) << 2;
  59. }
  60.  
  61. /* Compute the size of the bitmap storage, */
  62. /* including the space for the scan line pointer table. */
  63. /* Note that scan lines are padded to a multiple of 4 bytes. */
  64. ulong
  65. gdev_mem_bitmap_size(const gx_device_memory *dev)
  66. {    return (ulong)dev->height * (gdev_mem_raster(dev) + sizeof(byte *));
  67. }
  68.  
  69. /* 'Open' the memory device and create the scan line table. */
  70. int
  71. mem_open(gx_device *dev)
  72. {    byte *scan_line = mdev->base;
  73.     uint raster = mdev->raster = gdev_mem_raster((gx_device_memory *)dev);
  74.     byte **pptr = (byte **)(scan_line + (uint)dev->height * raster);
  75.     byte **pend = pptr + dev->height;
  76.     mdev->line_ptrs = pptr;
  77.     while ( pptr < pend )
  78.        {    *pptr++ = scan_line;
  79.         scan_line += raster;
  80.        }
  81.     mdev->bytes_le =
  82. #if arch_is_big_endian
  83.         0
  84. #else
  85.         /* NOTE: mem_mono_get_bits relies on the fact that */
  86.         /* sizeof(mono_chunk) == 2! */
  87.         (mdev->color_info.depth < 8 ? sizeof(mono_chunk) : 0);
  88. #endif
  89.         ;
  90.     return 0;
  91. }
  92.  
  93. /* Return the initial transformation matrix */
  94. void
  95. mem_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
  96. {    pmat->xx = mdev->initial_matrix.xx;
  97.     pmat->xy = mdev->initial_matrix.xy;
  98.     pmat->yx = mdev->initial_matrix.yx;
  99.     pmat->yy = mdev->initial_matrix.yy;
  100.     pmat->tx = mdev->initial_matrix.tx;
  101.     pmat->ty = mdev->initial_matrix.ty;
  102. }
  103.  
  104. /* Test whether a device is a memory device */
  105. int
  106. gs_device_is_memory(const gx_device *dev)
  107. {    /* We can't just compare the procs, or even an individual proc, */
  108.     /* because we might be tracing.  Compare the device name, */
  109.     /* and hope for the best. */
  110.     const char *name = dev->dname;
  111.     int i;
  112.     for ( i = 0; i < 6; i++ )
  113.       if ( name[i] != "image("[i] ) return 0;
  114.     return 1;
  115. }
  116.  
  117. /* Ensure that the data bytes are in big-endian order. */
  118. /* This is never called on big-endian platforms; */
  119. /* on little-endian platforms, the chunk size is ushort, */
  120. /* regardless of the size of an int. */
  121. void
  122. gdev_mem_ensure_byte_order(gx_device_memory *dev)
  123. {
  124. #if !arch_is_big_endian
  125.     if ( !dev->bytes_le ) return;    /* already in order */
  126.     memswab2(dev->base, dev->base, dev->raster * dev->height);
  127.     dev->bytes_le = 0;
  128. #endif
  129. }
  130.  
  131. /* Copy one or more scan lines to a client. */
  132. #undef chunk
  133. #define chunk byte
  134. int
  135. mem_get_bits(gx_device *dev, int y, byte *str, uint size, int pad_to_word)
  136. {    uint bytes_per_line =
  137.         gx_device_bytes_per_scan_line(dev, pad_to_word);
  138.     byte *src = scan_line_base(mdev, y);
  139.     byte *dest = str;
  140.     uint count = min(size / bytes_per_line, dev->height - y);
  141.     int swap = mdev->bytes_le;
  142.     if ( bytes_per_line == mdev->raster )
  143.        {    if ( swap && pad_to_word >= 0 )
  144.             memswab2(src, dest, bytes_per_line * count);
  145.         else
  146.             memcpy(dest, src, bytes_per_line * count);
  147.        }
  148.     else                /* know pad_to_word == 0 */
  149.        {    uint c;
  150.         for ( c = count; c-- != 0; )
  151.            {    if ( swap )
  152.                {    /* We have to take extra care if */
  153.                 /* bytes_per_line is odd. */
  154.                 if ( bytes_per_line & 1 )
  155.                    {    memswab2(src, dest, bytes_per_line - 1);
  156.                     dest[bytes_per_line - 1] =
  157.                       src[bytes_per_line];
  158.                    }
  159.                 else
  160.                     memswab2(src, dest, bytes_per_line);
  161.                }
  162.             else
  163.                 memcpy(dest, src, bytes_per_line);
  164.             src += mdev->raster;
  165.             dest += bytes_per_line;
  166.            }
  167.        }
  168.     return (swap && pad_to_word < 0 ? swap : 0);
  169. }
  170.  
  171. /* ------ Monochrome ------ */
  172.  
  173. /* Procedures */
  174. private dev_proc_copy_mono(mem_mono_copy_mono);
  175. private dev_proc_fill_rectangle(mem_mono_fill_rectangle);
  176.  
  177. /* The device descriptor. */
  178. private gx_device_procs mem_mono_procs =
  179.   mem_procs(gx_default_map_rgb_color, gx_default_map_color_rgb,
  180.     mem_mono_copy_mono, gx_default_copy_color, mem_mono_fill_rectangle);
  181.  
  182. /* The instance is public. */
  183. const gx_device_memory mem_mono_device =
  184.   mem_device("image(mono)", 1, mem_mono_procs);
  185.  
  186. /* Convert x coordinate to byte offset in scan line. */
  187. #define x_to_byte(x) ((x) >> 3)
  188.  
  189. /* Fill a rectangle with a color. */
  190. #undef chunk
  191. #define chunk mono_chunk
  192. private int
  193. mem_mono_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  194.   gx_color_index color)
  195. {    uint bit;
  196.     chunk right_mask;
  197.     byte fill;
  198.     declare_scan_ptr(dest);
  199.     check_rect();
  200.     setup_rect(dest);
  201. #define write_loop(stat)\
  202.  { int line_count = h;\
  203.    chunk *ptr = dest;\
  204.    do { stat; inc_chunk_ptr(ptr, draster); }\
  205.    while ( --line_count );\
  206.  }
  207. #define write_partial(msk)\
  208.    if ( fill ) write_loop(*ptr |= msk)\
  209.    else write_loop(*ptr &= ~msk)
  210.     switch ( color )
  211.        {
  212.     case 0: fill = mdev->invert; break;
  213.     case 1: fill = ~mdev->invert; break;
  214.     case gx_no_color_index: return 0;        /* transparent */
  215.     default: return -1;        /* invalid */
  216.        }
  217.     bit = x & chunk_bit_mask;
  218.     if ( bit + w <= chunk_bits )
  219.        {    /*
  220.          * Only one word. We have to split following statement
  221.          * because of a bug in the Xenix C compiler (it produces
  222.          * a signed rather than an unsigned shift if we don't
  223.          * split).
  224.          */
  225.         right_mask =
  226.           (w == chunk_bits ? chunk_all_bits : chunk_hi_bits(w));
  227.         right_mask >>= bit;
  228.        }
  229.     else
  230.        {    int byte_count;
  231.         if ( bit )
  232.            {    /* We have to split the following statement */
  233.             /* into two because of a bug in the DEC */
  234.             /* VAX/VMS C compiler. */
  235.             chunk mask = chunk_all_bits;
  236.             mask >>= bit;
  237.             write_partial(mask);
  238.             dest++;
  239.             w += bit - chunk_bits;
  240.            }
  241.         right_mask = chunk_hi_bits(w & chunk_bit_mask);
  242.         if ( (byte_count = (w >> 3) & -chunk_bytes) != 0 )
  243.            {    write_loop(memset(ptr, fill, byte_count));
  244.             inc_chunk_ptr(dest, byte_count);
  245.            }
  246.        }
  247.     if ( right_mask )
  248.         write_partial(right_mask);
  249.     return 0;
  250. }
  251.  
  252. /* Copy a monochrome bitmap. */
  253.  
  254. /* Fetch a chunk from the source. */
  255. /* Note that the source data are always stored big-endian. */
  256. /* Note also that the macros always cast cptr, */
  257. /* so it doesn't matter what the type of cptr is. */
  258. #undef chunk
  259. #if arch_is_big_endian
  260. #  define chunk uint
  261. #  define cfetch(cptr) (*(chunk *)(cptr))
  262. #else
  263. #  define chunk ushort
  264. #  define cfetch(cptr) (((chunk)*(byte *)(cptr) << 8) + ((byte *)(cptr))[1])
  265. #endif
  266. /* Fetch a chunk that straddles a chunk boundary. */
  267. /***
  268. #if arch_is_big_endian
  269. ***/
  270. #  define cfetch2(cptr, cskew, skew)\
  271.     ((cfetch(cptr) << cskew) + (cfetch((chunk *)(cptr) + 1) >> skew))
  272. /***
  273. #else
  274. #  define cfetch2(cptr, cskew, skew)\
  275.     (cskew <= 8 ?\
  276.      (cfetch(cptr) << cskew) + (((byte *)(cptr))[2] >> (skew - 8)) :\
  277.      (((byte *)(cptr))[1] << cskew) + (cfetch((chunk *)(cptr) + 1) >> skew))
  278. #endif
  279. ***/
  280.  
  281. /* copy_function and copy_shift get added together for dispatch */
  282. typedef enum {
  283.     copy_or = 0, copy_store, copy_and, copy_funny
  284. } copy_function;
  285. /* copy_right/left is not an enum, because compilers complain about */
  286. /* an enumeration clash when these are added to a copy_function. */
  287. #define copy_right ((copy_function)0)
  288. #define copy_left ((copy_function)4)
  289. typedef struct {
  290.     short invert;
  291.     ushort op;            /* copy_function */
  292. } copy_mode;
  293. /* Map from <c0,c1,invert> to copy_mode. */
  294. #define cm(i,op) { i, (ushort)op }
  295. private copy_mode copy_modes[9*2] = {
  296.     cm(-1, copy_funny),        /* NN */
  297.     cm(-1, copy_and),        /* N0 */
  298.     cm(0, copy_or),            /* N1 */
  299.     cm(0, copy_and),        /* 0N */
  300.     cm(0, copy_funny),        /* 00 */
  301.     cm(0, copy_store),        /* 01 */
  302.     cm(-1, copy_or),        /* 1N */
  303.     cm(-1, copy_store),        /* 10 */
  304.     cm(0, copy_funny),        /* 11 */
  305.     cm(-1, copy_funny),        /* NNi */
  306.     cm(0, copy_or),            /* N1i */
  307.     cm(-1, copy_and),        /* N0i */
  308.     cm(-1, copy_or),        /* 1Ni */
  309.     cm(0, copy_funny),        /* 11i */
  310.     cm(-1, copy_store),        /* 10i */
  311.     cm(0, copy_and),        /* 0Ni */
  312.     cm(0, copy_store),        /* 01i */
  313.     cm(0, copy_funny)        /* 00i */
  314. };
  315. private int
  316. mem_mono_copy_mono(gx_device *dev,
  317.   const byte *base, int sourcex, int sraster, gx_bitmap_id id,
  318.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  319. {    register const byte *bptr;        /* actually chunk * */
  320.     int dbit, wleft;
  321.     uint mask;
  322.     copy_mode mode;
  323. #define function (copy_function)(mode.op)
  324.     declare_scan_ptr_as(dbptr, byte *);
  325. #define optr ((chunk *)dbptr)
  326.     register int skew;
  327.     register uint invert;
  328.     check_rect();
  329. #if gx_no_color_value != -1        /* hokey! */
  330.     if ( zero == gx_no_color_index ) zero = -1;
  331.     if ( one == gx_no_color_index ) one = -1;
  332. #endif
  333. #define izero (int)zero
  334. #define ione (int)one
  335.     mode =
  336.       copy_modes[(mdev->invert & 9) + izero + izero + izero + ione + 4];
  337. #undef izero
  338. #undef ione
  339.     invert = (uint)(int)mode.invert;    /* load register */
  340.     setup_rect_as(dbptr, byte *);
  341.     bptr = base + ((sourcex & ~chunk_bit_mask) >> 3);
  342.     dbit = x & chunk_bit_mask;
  343.     skew = dbit - (sourcex & chunk_bit_mask);
  344.     /* We have to split the following statement */
  345.     /* into two because of a bug in the DEC */
  346.     /* VAX/VMS C compiler. */
  347.     mask = chunk_all_bits;
  348.     mask >>= dbit;
  349. /* Macros for writing partial chunks. */
  350. /* The destination pointer is always named optr, */
  351. /* and must be declared as chunk *. */
  352. /* cinvert may be temporarily redefined. */
  353. #define cinvert(bits) ((bits) ^ invert)
  354. #define write_or_masked(bits, mask, off)\
  355.   optr[off] |= (cinvert(bits) & mask)
  356. #define write_store_masked(bits, mask, off)\
  357.   optr[off] = ((optr[off] & ~mask) | (cinvert(bits) & mask))
  358. #define write_and_masked(bits, mask, off)\
  359.   optr[off] &= (cinvert(bits) | ~mask)
  360. /* Macros for writing full chunks. */
  361. #define write_or(bits)  *optr |= cinvert(bits)
  362. #define write_store(bits) *optr = cinvert(bits)
  363. #define write_and(bits) *optr &= cinvert(bits)
  364. /* Macro for incrementing to next chunk. */
  365. #define next_x_chunk\
  366.   bptr += chunk_bytes; dbptr += chunk_bytes
  367. /* Common macro for the end of each scan line. */
  368. #define end_y_loop(sdelta, ddelta)\
  369.   if ( --h == 0 ) break;\
  370.   bptr += sdelta; dbptr += ddelta
  371.     if ( (wleft = w + dbit - chunk_bits) <= 0 )
  372.        {    /* The entire operation fits in one (destination) chunk. */
  373.         /* Some machines can't handle w == chunk_bits! */
  374. #if arch_cant_shift_full_chunk
  375.         if ( w != chunk_bits )
  376. #endif
  377.           mask -= mask >> w;
  378. #define write_single(wr_op, src)\
  379.   for ( ; ; )\
  380.    { wr_op(src, mask, 0);\
  381.      end_y_loop(sraster, draster);\
  382.    }
  383. #define write1_loop(src)\
  384.   switch ( function ) {\
  385.     case copy_or: write_single(write_or_masked, src); break;\
  386.     case copy_store: write_single(write_store_masked, src); break;\
  387.     case copy_and: write_single(write_and_masked, src); break;\
  388.     default: goto funny;\
  389.   }
  390.         if ( skew >= 0 )    /* single -> single, right/no shift */
  391.            {    write1_loop(cfetch(bptr) >> skew);
  392.            }
  393.         else if ( wleft <= skew )    /* single -> single, left shift */
  394.            {    skew = -skew;
  395.             write1_loop(cfetch(bptr) << skew);
  396.            }
  397.         else            /* double -> single */
  398.            {    int cskew = -skew;
  399.             skew += chunk_bits;
  400.             write1_loop(cfetch2(bptr, cskew, skew));
  401.            }
  402. #undef write1_loop
  403. #undef write_single
  404.        }
  405.     else if ( wleft <= skew )
  406.        {    /* 1 source chunk -> 2 destination chunks. */
  407.         /* This is an important special case for */
  408.         /* both characters and halftone tiles. */
  409.         register uint bits;
  410.         uint rmask = chunk_hi_bits(wleft);
  411.         int cskew = chunk_bits - skew;
  412. #define write_1to2(wr_op)\
  413.   for ( ; ; )\
  414.    { bits = cfetch(bptr) ^ invert;\
  415.      wr_op(bits >> skew, mask, 0);\
  416.      wr_op(bits << cskew, rmask, 1);\
  417.      end_y_loop(sraster, draster);\
  418.    }
  419. #undef cinvert
  420. #define cinvert(bits) (bits)        /* pre-inverted here */
  421.         switch ( function )
  422.            {
  423.         case copy_or: write_1to2(write_or_masked); break;
  424.         case copy_store: write_1to2(write_store_masked); break;
  425.         case copy_and: write_1to2(write_and_masked); break;
  426.         default: goto funny;
  427.            }
  428. #undef cinvert
  429. #define cinvert(bits) ((bits) ^ invert)
  430. #undef write_1to2
  431.        }
  432.     else
  433.        {    /* More than one source chunk and more than one */
  434.         /* destination chunk are involved. */
  435.         uint rmask = chunk_hi_bits(wleft & chunk_bit_mask);
  436.         int words = (wleft & ~chunk_bit_mask) >> 3;
  437.         uint sskip = sraster - words;
  438.         uint dskip = draster - words;
  439.         register uint bits;
  440.         if ( skew == 0 )    /* optimize the aligned case */
  441.            {
  442. #define write_aligned(wr_op, wr_op_masked)\
  443.   for ( ; ; )\
  444.    { int count = wleft;\
  445.      /* Do first partial chunk. */\
  446.      wr_op_masked(cfetch(bptr), mask, 0);\
  447.      /* Do full chunks. */\
  448.      while ( (count -= chunk_bits) >= 0 )\
  449.       { next_x_chunk; wr_op(cfetch(bptr)); }\
  450.      /* Do last chunk */\
  451.      if ( count > -chunk_bits )\
  452.       { wr_op_masked(cfetch(bptr + chunk_bytes), rmask, 1); }\
  453.      end_y_loop(sskip, dskip);\
  454.    }
  455.             switch ( function )
  456.               {
  457.               case copy_or:
  458.                 write_aligned(write_or, write_or_masked);
  459.                 break;
  460.               case copy_store:
  461.                 write_aligned(write_store, write_store_masked);
  462.                 break;
  463.               case copy_and:
  464.                 write_aligned(write_and, write_and_masked);
  465.                 break;
  466.               default:
  467.                 goto funny;
  468.               }
  469. #undef write_aligned
  470.            }
  471.         else            /* not aligned */
  472.            {    int ccase =
  473.               (skew >= 0 ? copy_right :
  474.                ((bptr += chunk_bytes), copy_left))
  475.               + (int)function;
  476.             int cskew = -skew & chunk_bit_mask;
  477.             skew &= chunk_bit_mask;
  478.             for ( ; ; )
  479.                {    int count = wleft;
  480. #define prefetch_right\
  481.   bits = cfetch(bptr) >> skew
  482. #define prefetch_left\
  483.   bits = cfetch2(bptr - chunk_bytes, cskew, skew)
  484. #define write_unaligned(wr_op, wr_op_masked)\
  485.   wr_op_masked(bits, mask, 0);\
  486.   /* Do full chunks. */\
  487.   while ( count >= chunk_bits )\
  488.     { bits = cfetch2(bptr, cskew, skew);\
  489.       next_x_chunk; wr_op(bits); count -= chunk_bits;\
  490.     }\
  491.   /* Do last chunk */\
  492.   if ( count > 0 )\
  493.     { bits = cfetch(bptr) << cskew;\
  494.       if ( count > skew ) bits += cfetch(bptr + chunk_bytes) >> skew;\
  495.       wr_op_masked(bits, rmask, 1);\
  496.     }
  497.                 switch ( ccase )
  498.                   {
  499.                   case copy_or + copy_left:
  500.                     prefetch_left; goto uor;
  501.                   case copy_or + copy_right:
  502.                     prefetch_right;
  503. uor:                    write_unaligned(write_or, write_or_masked);
  504.                     break;
  505.                   case copy_store + copy_left:
  506.                     prefetch_left; goto ustore;
  507.                   case copy_store + copy_right:
  508.                     prefetch_right;
  509. ustore:                    write_unaligned(write_store, write_store_masked);
  510.                     break;
  511.                   case copy_and + copy_left:
  512.                     prefetch_left; goto uand;
  513.                   case copy_and + copy_right:
  514.                     prefetch_right;
  515. uand:                    write_unaligned(write_and, write_and_masked);
  516.                     break;
  517.                   default:
  518.                     goto funny;
  519.                   }
  520.                 end_y_loop(sskip, dskip);
  521. #undef write_unaligned
  522. #undef prefetch_left
  523. #undef prefetch_right
  524.                }
  525.            }
  526.        }
  527. #undef end_y_loop
  528. #undef next_x_chunk
  529.     return 0;
  530.     /* Handle the funny cases that aren't supposed to happen. */
  531. funny:    return (invert ? -1 : mem_mono_fill_rectangle(dev, x, y, w, h, zero));
  532. #undef optr
  533. }
  534.